/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.communicate;

import com.ssm.changeunit.ChangeLog;
import com.ssm.changeunit.ChangeUnitException;
import com.ssm.changeunit.ChangeUnitItem;
import com.ssm.changeunit.Environment;
import com.ssm.changeunit.persistance.*;
//import com.ssm.changeunit.persistance.BranchDAO;
//import com.ssm.changeunit.persistance.ChangeUnitBranchDAO;
//import com.ssm.changeunit.persistance.ChangeUnitBranchDTO;
//import com.ssm.changeunit.persistance.ChangeUnitDTO;
//import com.ssm.changeunit.persistance.ChangeUnitItemDTO;
//import com.ssm.changeunit.persistance.ChildDTO;
//import com.ssm.changeunit.persistance.ParentDTO;
//import com.ssm.changeunit.persistance.PersistanceException;
import com.ssm.changeunit.util.ChangeUnitTraveler;
import com.ssm.changeunit.util.FileUtil;
import com.ssm.changeunit.util.ZipUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 시리우스
 */

public class ChangeUnitRPC {
    private List<String> getChangeUnitIds(String startChangeUnitId, String endChangeUnitId) throws ChangeUnitException {
        List<String> changeUnitIdList = new ArrayList<String>();
        
        ChangeUnitTraveler changeUnitTraveler = new ChangeUnitTraveler(ChangeUnitTraveler.Direct.Child, startChangeUnitId, null);
        changeUnitTraveler.init();
        String changeUnitId = "";
 
        while(changeUnitTraveler.hasNext()){
                changeUnitId = changeUnitTraveler.getNextChangeUnit().getChangeUnitId();
                changeUnitIdList.add(changeUnitId);
                if(changeUnitId.equals(endChangeUnitId))    break;
        }
        
        return changeUnitIdList;
    }
    
    public String getCurrentChangeUnitIdOfBranch(String branchName) throws ChangeUnitException {
        String return_value ="";
        
        BranchDAO branchDAO = new BranchDAO();
        try{
            return_value = branchDAO.find(branchName).getChangeUnitId();
         }
         catch(PersistanceException ex) {
             throw new ChangeUnitException(ex);
         }
        return return_value;
    }
    
    public boolean isChangeUnitExists(String branchName, String changeUnitId) throws ChangeUnitException {
        
        boolean return_value = false;
        
        ChangeUnitBranchDAO changeUnitBranchDAO = new ChangeUnitBranchDAO();
        try{
            List<ChangeUnitBranchDTO> changeUnitBranchDTOList = changeUnitBranchDAO.findByBranchName(branchName);
            
            for(ChangeUnitBranchDTO item : changeUnitBranchDTOList){
                if(item.getChangeUnitId().equals(changeUnitId)){
                    return_value = true;
                    break;
                }
            }
            
        }
        catch(PersistanceException ex) {
            throw new ChangeUnitException(ex);
        }
        return return_value;
    }
    
    public ChangeUnitRPCResult getChangeUnitRPCResult(String startChangeUnitId, String endChangeUnitId, String branchName) throws ChangeUnitException {
        
        ChangeUnitRPCResult changeUnitRPCResult = new ChangeUnitRPCResult();
        
        try{
            List<String> changeUnitIds = getChangeUnitIds(startChangeUnitId,endChangeUnitId);
            
            //DAO new
            ChangeUnitBranchDAO changeUnitBranchDAO = new ChangeUnitBranchDAO();
            ParentDAO parentDAO = new ParentDAO();
            ChildDAO childDAO = new ChildDAO();
            ChangeUnitDAO changeUnitDAO = new ChangeUnitDAO();
            ChangeUnitItemDAO changeUnitItemDAO = new ChangeUnitItemDAO();
            ChangeLogDAO changeLogDAO = new ChangeLogDAO();

            List<ChangeUnitDTO> changeUnitDTOs = new ArrayList<ChangeUnitDTO>();
            List<ChangeLogDTO> changeLogDTOs = new ArrayList<ChangeLogDTO>();
            
            //startChangeUnitId 에 대응하는  ChangeUnitItem 목록을 가져오기
            List<ChangeUnitItemDTO> existChangeUnitItemDTOs = new ArrayList<ChangeUnitItemDTO>();
            existChangeUnitItemDTOs = changeUnitItemDAO.findByChangeUnitId(startChangeUnitId);
            
            //List<String> type 으로 저장
            List<String> existChangeUnitItem = new ArrayList<String>();
            for(ChangeUnitItemDTO item : existChangeUnitItemDTOs){
                existChangeUnitItem.add(item.getChangeUnitId());
            }
            
            //changeUnitIds 에 연관되는 branch 정보를 DTO 로 가져옴 
            List<ChangeUnitBranchDTO> realateChangeUnitBranchDTOs = new ArrayList<ChangeUnitBranchDTO>();
            for(String item : changeUnitIds){
                realateChangeUnitBranchDTOs = changeUnitBranchDAO.findByChangeUnitId(item);
            }
            
            //List<String> type 으로 저장
            List<String> realateChangeUnitBranch = new ArrayList<String>();
            for(ChangeUnitBranchDTO item : realateChangeUnitBranchDTOs){
                realateChangeUnitBranch.add(item.getChangeUnitId());
            }
            
            for(String item : changeUnitIds){
                
                //Parent
                if(realateChangeUnitBranch.contains(item)){
                    changeUnitRPCResult.setParentDTOs(parentDAO.findByChangeUnitId(item));
                }

                //Child
                if(realateChangeUnitBranch.contains(item)){
                  changeUnitRPCResult.setChildDTOs(childDAO.findByChangeUnitId(item));
                }
                
                //ChangeUnit
                changeUnitDTOs.add(changeUnitDAO.find(item));
                changeUnitRPCResult.setChangeUnitDTOs(changeUnitDTOs);
                
                List<ChangeUnitItemDTO> changeUnitItemDTOs = changeUnitItemDAO.findByChangeUnitId(item);
                
                //ChangeUnitItem
                changeUnitRPCResult.setChangeUnitItemsDTOs(changeUnitItemDTOs);
              
                for(ChangeUnitItemDTO changeUnitItem : changeUnitItemDTOs) {
                    ChangeLogDTO changeLogDTO = changeLogDAO.find(changeUnitItem.getRefChangeLogId());
                    
                    //if(!existChangeUnitItem.contains(changeLogDTO.getChangeLogId())){
                        changeLogDTOs.add(changeLogDTO);
                    //}
                }
                
                changeUnitRPCResult.setChangeLogDTOs(changeLogDTOs);
            }
        }
        catch(PersistanceException ex) {
            throw new ChangeUnitException(ex);
        }
        return changeUnitRPCResult;
    }
    
    public void push(ChangeUnitRPCResult changeUnitRPCResult) throws ChangeUnitException {
        
        try{
            //DAO new
            ParentDAO parentDAO = new ParentDAO();
            ChildDAO childDAO = new ChildDAO();
            ChangeUnitDAO changeUnitDAO = new ChangeUnitDAO();
            ChangeUnitItemDAO changeUnitItemDAO = new ChangeUnitItemDAO();
            ChangeLogDAO changeLogDAO = new ChangeLogDAO();
            
  
            
            //Parent
            for(ParentDTO item : changeUnitRPCResult.getParentDTOs()){
                parentDAO.insert(item);
            }
            
            //Child
            for(ChildDTO item : changeUnitRPCResult.getChildDTOs()){
                childDAO.insert(item);
            }
            
            //ChangeUnit
            for(ChangeUnitDTO item : changeUnitRPCResult.getChangeUnitDTOs()){
                changeUnitDAO.insert(item);
            }
            
            //ChangeUnitItem
            for(ChangeUnitItemDTO item : changeUnitRPCResult.getChangeUnitItemsDTOs()){
                changeUnitItemDAO.insert(item);
            }
            
            //ChangeLog
            for(ChangeLogDTO item : changeUnitRPCResult.getChangeLogDTOs()){
                changeLogDAO.insert(item);
            }

        }
        catch(PersistanceException ex) {
            throw new ChangeUnitException(ex);
        }
        
    }
    
    public byte[] getDatabase() throws ChangeUnitException {
        try {
            //ZipUtil.createZipFile(Environment.DATABASE_NAME, Environment.ROOT_DIR + "db.zip", true);
            ZipUtil.createZipFile("c:\\temp\\db", "c:\\temp\\db.zip", true);
            
            System.out.println("왜이래");
            
            return FileUtil.readFromFileToByte("c:\\temp\\db.zip");
        }
        catch(Exception ex) {
            throw new ChangeUnitException("데이터베이스 압축 에러.");
        }
        finally {
            FileUtil.delete("c:\\temp\\db.zip");
        }
    }
}